package editor;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ToolPanel extends JPanel implements ActionListener
{
	private ArrayList<ToolButton> buttons = new ArrayList<ToolButton>();
	
	private CaptionTextArea textPanel;
	private JCheckBox boxTool;
	private JSlider colorSlide;
	private JTextField title;
	private LevelEditor editor;
	private int x = 90;
	private int y = 30;
	
	public ToolPanel(LevelEditor e, CaptionTextArea t) 
	{
		textPanel = t;
		setLayout(null);
		buttons.add(new ToolButton("Blue", textPanel));
		buttons.add(new ToolButton("Black", textPanel));
		buttons.add(new ToolButton("Red", textPanel));
		buttons.add(new ToolButton("Gray", textPanel));
		buttons.add(new ToolButton("Green", textPanel));
		buttons.add(new ToolButton("Brown", textPanel));
		buttons.add(new ToolButton("Cyan", textPanel));
		buttons.add(new ToolButton("Test Map", textPanel));
		buttons.add(new ToolButton("Save", textPanel));
		buttons.add(new ToolButton("Menu", textPanel));
		boxTool = new JCheckBox("Box Tool");
		colorSlide = new JSlider(20, 255, 255);
		title = new JTextField("Map Name");
		
		
		buttons.get(0).setBackground(Color.blue);
		buttons.get(0).setForeground(Color.blue);

		buttons.get(1).setBackground(Color.black);
		buttons.get(1).setForeground(Color.black);

		buttons.get(2).setBackground(Color.red);
		buttons.get(2).setForeground(Color.red);

		buttons.get(3).setBackground(Color.gray);
		buttons.get(3).setForeground(Color.gray);

		buttons.get(4).setBackground(Color.green);
		buttons.get(4).setForeground(Color.green);

		buttons.get(5).setBackground(new Color(128, 64, 0));
		buttons.get(5).setForeground(new Color(128, 64, 0));
		
		buttons.get(6).setBackground(Color.cyan);
		buttons.get(6).setForeground(Color.cyan);
		
		buttons.get(7).setBackground(new Color(-23456));
		
		buttons.get(8).setBackground(new Color(-40000));
		
		buttons.get(9).setBackground(new Color(-30000));
		
		for(int i=0; i<buttons.size(); i++)
		{
			buttons.get(i).setSize(x, y);
			if(i < 7)
			{
				buttons.get(i).setLocation(5, 20+i*30);
			}
			else
			{
				buttons.get(i).setLocation(5, 200+i*40);
			}
			buttons.get(i).addActionListener(this);
			//buttons.get(i).setBorderPainted(false);
			add(buttons.get(i));
		}
		title.setSize(90, 30);
		title.setLocation(5, 400);
		boxTool.setBackground(Color.black);
		boxTool.setForeground(Color.white);
		boxTool.setSize(80, 30);
		boxTool.setLocation(10, 310);
		colorSlide.setBackground(Color.black);
		colorSlide.setForeground(Color.white);
		colorSlide.setSize(80, 30);
		colorSlide.setLocation(10, 250);
		boxTool.addActionListener(this);
		add(title);
		add(boxTool);
		//add(colorSlide);
		editor = e;
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == boxTool)
		{
			editor.changeColor(-20000);
		}
		else
		{
			JButton button = (JButton) e.getSource();
			editor.changeColor(button.getBackground().getRGB());
			textPanel.setSelected(button.getText());
			if(button.getBackground().getRGB() == -40000)
			{
				editor.saveLevel(title.getText());
			}
			else
			{
				setBackground(button.getBackground());
				boxTool.setBackground(button.getBackground());
				colorSlide.setBackground(button.getBackground());
			}
		}
	}

	public void removeListeners() 
	{
		for(int i=0; i<buttons.size(); i++)
		{
			buttons.get(i).removeActionListener(this);
		}
		boxTool.removeActionListener(this);
		
	}
}
